package com.ilancuo.datastructuresandalgorithmsinjava.ch04_stack_list;

/**
 * Created by ilancuo on 2018/8/22.
 */
public class PriorityQ {
    private int maxSize;
    private long[] queryArray;
    private int nItems;

    public PriorityQ(int s) {
        maxSize = s;
        queryArray = new long[maxSize];
        nItems = 0;
    }

    public void insert(long item) {
        int j;
        if (nItems == 0)
            queryArray[nItems++] = item;
        else {
            for (j = nItems - 1; j >= 0; j--) {
                if (item > queryArray[j])
                    queryArray[j + 1] = queryArray[j];
                else
                    break;
            }
            queryArray[j + 1] = item;
            nItems++;
        }
    }

    public long remove() {
        return queryArray[--nItems];
    }

    public long peekMin() {
        return queryArray[nItems - 1];
    }

    public boolean isEmpty() {
        return (nItems == 0);
    }

    public boolean isFull() {
        return (nItems == maxSize);
    }
}

class PriorityQApp {
    public static void main(String[] args) {
        PriorityQ thePQ = new PriorityQ(5);
        thePQ.insert(30);
        thePQ.insert(50);
        thePQ.insert(10);
        thePQ.insert(40);
        thePQ.insert(20);

        while (!thePQ.isEmpty()) {
            long item = thePQ.remove();
            System.out.print(item + " ");
        }
        System.out.println();
    }
}
